<?php

namespace Action;

class Register extends \Library\Action\PortalAction {

	private $_messages = array();
	private $_tabPane;

	public function execute() {
		parent::execute();
		$this->setTitle( 'Register' );
		
		if( !$this->hasParam( 'Action_Register' ) ) {
			if( $this->hasParam( 'Action_Authenticate' ) ) {
				die( $this->action( 'Authenticate', $this->getParams() ) );
			}
			return;
		}
		
		$auth = $this->_handle( $this->getParams() );
		
		if( $this->hasFailed() ) {
			return;
		}
		
		die( $this->action( 'Authenticate', $auth ) );
	}
	
	public function getTabPane() {
		if( $this->_tabPane === null ) {
			$this->_tabPane = new \Library\Widget\TabPane();
			$this->_tabPane
				->getTab( 'basic' )
					->setTitle( 'By password' )
					->setContent( _view( 'New', \Library\Auth::get( 'Basic' ) ) )
					->getTabPane()
				->getTab( 'openid' )
					->setTitle( 'By OpenID' )
					->setContent( _view( 'New', \Library\Auth::get( 'OpenID' ) ) )
					->getTabPane();
		}
		return $this->_tabPane;
	}

	public function getEmail() {
		return isset($_POST['email']) ? $_POST['email'] : '';
	}

	public function getFirstName() {
		return isset($_POST['first_name']) ? $_POST['first_name'] : '';
	}
	
	public function getLastName() {
		return isset($_POST['last_name']) ? $_POST['last_name'] : '';
	}
	
	public function hasAgreement() {
		return isset($_POST['agreement']) ? $_POST['agreement'] : false;
	}
	
	public function hasFailed() {
		return count($this->_messages) > 0;
	}
	
	public function getMessages() {
		return $this->_messages;
	}
	
	private function _handle( $post ) {
	
		foreach($post as $k => $v) {
			if( !is_array($v) ) {
				$post[$k] = trim($v);
			}
		}

		$check = \Library\Checker\FirstNameChecker::check( $post['first_name'] );
		if( $check !== true ) {
			$this->_messages[] = $check;
		}

		$check = \Library\Checker\LastNameChecker::check( $post['last_name'] );
		if( $check !== true ) {
			$this->_messages[] = $check;
		}
		
		$check = \Library\Checker\EmailChecker::check( $post['email'] );
		if( $check !== true ) {
			$this->_messages[] = $check;
		}

		if( $this->_messages ) {
			return false;
		}

		$result = $this->action( 'User\CheckEmail', $post );
		if( !$result['valid'] ) {
			$this->_messages[] = $result['message'];
		}

		if( !$post['agreement'] ) {
			$this->_messages[] = 'You must agree with terms of use';
		}
		
		$post['auth'] = array_filter( $post['auth'], array( $this, '_isFilledIn' ) );
		
		if( !$post['auth'] ) {
			$this->_messages[] = 'You must choose an authentication method';
		}
		
		foreach( $post['auth'] as $auth ) {
			$a = \Library\Auth::get( $auth['kind'] );
			try {
				$a->check( $auth );
			}
			catch( \Exception $e ) {
				$this->_messages[] = $e->getMessage();
			}
		}
		
		if( $this->_messages ) {
			return false;
		}
		
		$now = _now();
		
		$flow_id = _q()->set1( 'posts_flows', array(
			'type' => 'User' ) );
		
		$user_id = _q()->set1( 'users', array(
			'email' => $post['email'],
			'flow_id' => $flow_id,
			'first_name' => $post['first_name'],
			'last_name' => $post['last_name'],
			'creation_time' => $now,
			'connection_time' => $now ) );
		
		foreach( $post['auth'] as $auth ) {
			$a = \Library\Auth::get( $auth['kind'] );
			$auth['user_id'] = $user_id;
			$a->create( $auth );
		}
	
		if( $post['auth'][0]['kind'] == 'Basic' ) {
			$post['auth'][0]['email'] = $post['email'];
		}
		$post['Action_Authenticate'] = true;
		
		return $post;
	}
	
	private function _isFilledIn( $data ) {
	
		_a( preg_match( '/^[A-Za-z]+$/', $data['kind'] ), 'Invalid authentication method' );
		unset( $data['kind'] );
		foreach( $data as $k => $v ) {
			if( !$v ) {
				unset( $data[$k] );
			}
		}
		return $data ? true : false;
	}

}
